import {Platform, ToastController, NavController,} from 'ionic-angular';


export class RootPage {

  canExit: boolean = false;
  inRoot: boolean = true;
  platform: any
  toast:any
  navCtrl:any
  constructor() {
    this.platform = Platform
    this.toast = ToastController
    this.navCtrl = NavController
  }

  ionViewDidEnter() {
    this.viewDidEnter()
    this.inRoot = true
    this.platform.registerBackButtonAction(() => {
      if(this.inRoot){
        if (this.canExit) {
          this.platform.exitApp();
        } else {
          this.canExit = true
          let msg = this.toast.create({
            message: '再按一次退出应用',
            position: 'middle',
            duration: 2000
          })
          msg.present()
          msg.onDidDismiss(() => {
            this.canExit = false
          });
        }
      }else{
        this.navCtrl.pop()
      }
    },101)
  }

  ionViewWillLeave() {
    this.viewWillLeave()
    this.inRoot = false
  }

  viewWillLeave(){}
  viewDidEnter(){}
}
